package cn.edu.dgut.sai;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author Sai
 * Created by Sai on 2019-03-10.
 */
@WebFilter(filterName = "ShowHeaderFilter")
public class ShowHeaderFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {

        System.out.println(this.getClass());

        // 遍历http请求的header
        Enumeration headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = (String) headerNames.nextElement();
            System.out.println(headerName + ":" + req.getHeader(headerName));
        }

        // 必需加这句，才能继续执行过滤器链
        chain.doFilter(req,res);
    }
}
